package com.handy.vo;

import com.handy.constants.ErrorEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 返回对象
 *
 * @author handy
 */
@Data
@ApiModel(value = "返回对象")
public class ResultVo<T> implements Serializable {
    @ApiModelProperty(value = "返回编码")
    private String code;
    @ApiModelProperty(value = "返回消息")
    private String msg;
    @ApiModelProperty(value = "返回数据")
    private T data;

    private ResultVo(ErrorEnum errorEnum) {
        this.code = errorEnum.getCode();
        this.msg = errorEnum.getMsg();
    }

    private ResultVo(ErrorEnum errorEnum, String msg) {
        this.code = errorEnum.getCode();
        this.msg = msg;
    }

    private ResultVo(ErrorEnum errorEnum, T data) {
        this.code = errorEnum.getCode();
        this.msg = errorEnum.getMsg();
        this.data = data;
    }

    private ResultVo(ErrorEnum errorEnum, String msg, T data) {
        this.code = errorEnum.getCode();
        this.msg = msg;
        this.data = data;
    }

    public static <T> ResultVo<T> success() {
        return new ResultVo<>(ErrorEnum.SUCCESS);
    }

    public static <T> ResultVo<T> success(T data) {
        return new ResultVo<>(ErrorEnum.SUCCESS, data);
    }

    public static <T> ResultVo<T> success(String msg) {
        return new ResultVo<>(ErrorEnum.SUCCESS, msg);
    }

    public static <T> ResultVo<T> success(String msg, T data) {
        return new ResultVo<>(ErrorEnum.SUCCESS, msg, data);
    }

    public static <T> ResultVo<T> failure(ErrorEnum errorEnum) {
        return new ResultVo<>(errorEnum);
    }

    public static <T> ResultVo<T> failure(String msg) {
        return new ResultVo<>(ErrorEnum.OPERATION_FAILED, msg);
    }

    public static <T> ResultVo<T> failure(ErrorEnum errorEnum, String msg) {
        return new ResultVo<>(errorEnum, msg);
    }

    public static <T> ResultVo<T> failure(ErrorEnum errorEnum, T data) {
        return new ResultVo<>(errorEnum, data);
    }

}